package com.todo.delicacy.application.backend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.BizPageResponse;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.delicacy.application.backend.service.FoodInfoBackendService;
import com.todo.delicacy.application.convert.FoodConvert;
import com.todo.delicacy.application.entity.bo.FoodInfoBO;
import com.todo.delicacy.application.entity.condition.FoodInfoConditionBO;
import com.todo.delicacy.application.service.FoodInfoService;
import com.todo.delicacy.iface.backend.request.*;
import com.todo.delicacy.iface.backend.response.BaseFoodInfoResp;
import com.todo.delicacy.iface.backend.response.QueryRestaurantInfoByConditionRsp;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * (FoodInfo)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:46
 */
@Slf4j
@Service("foodInfoBackendService")
public class FoodInfoBackendServiceImpl implements FoodInfoBackendService {

    @Resource
    private FoodInfoService foodInfoService;


    @Override
    public BizResponse<BaseFoodInfoResp> queryFoodInfoById(QueryFoodInfoByIdReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        FoodInfoBO foodInfoBO = foodInfoService.queryFoodInfoById(req.getId());
        log.info("FoodInfoBackendService#queryFoodInfoById 根据id查询美食完成，入参：【{}】，出参：【{}】", JSONObject.toJSONString(req), JSONObject.toJSONString(foodInfoBO));
        return BizResponse.success(FoodConvert.convertToBaseFoodInfoResp(foodInfoBO));
    }

    @Override
    public BizPageResponse<QueryRestaurantInfoByConditionRsp> queryFoodInfoByCondition(QueryFoodInfoByConditionReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        FoodInfoConditionBO conditionBO = FoodConvert.convertToFoodInfoConditionBO(req);
        PageResult<FoodInfoBO> result = foodInfoService.queryFoodInfoByCondition(conditionBO);
        if (result == null) {
            log.error("FoodInfoBackendService#queryFoodInfoByCondition 多条件查询美食列表完成，数据为空 req:【{}】", JSONObject.toJSONString(req));
            return BizPageResponse.success();
        }
        return BizPageResponse.success(FoodConvert.convertToQueryRestaurantInfoByConditionRspPage(result));
    }

    @Override
    public BizResponse<Boolean> updateFoodInfo(UpdateFoodInfoReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        FoodInfoBO foodInfoBO = FoodConvert.convertToFoodInfoBO(req);
        Boolean updateResult = foodInfoService.updateFoodInfo(foodInfoBO);
        log.info("FoodInfoBackendService#updateFoodInfo 更新美食完成，入参：【{}】，出参：【{}】", JSONObject.toJSONString(req), updateResult);
        return BizResponse.success(updateResult);
    }

    @Override
    public BizResponse<Boolean> insertFoodInfo(InsertFoodInfoReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        FoodInfoBO foodInfoBO = FoodConvert.convertToFoodInfoBO(req);
        Boolean insertResult = foodInfoService.insertFoodInfo(foodInfoBO);
        log.info("FoodInfoBackendService#insertFoodInfo 插入美食完成，入参：【{}】，出参：【{}】", JSONObject.toJSONString(req), insertResult);
        return BizResponse.success(insertResult);
    }

    @Override
    public BizResponse<Boolean> removeFoodInfoById(RemoveFoodInfoReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        Boolean removeResult = foodInfoService.removeFoodInfoById(req.getFoodId());
        log.info("FoodInfoBackendService#removeFoodInfoById 删除美食信息完成，入参：【{}】，出参：【{}】", JSONObject.toJSONString(req), removeResult);
        return BizResponse.success(removeResult);
    }
}
